// ----------------------------------
// RSDK Project: Sonic Essence
// Script Description: Trick Ramp
// Script Author: Tails LP
// ----------------------------------

//-------Aliases-------//

private alias object.value0 : object.timer
private alias object.value1 : trickRamp.Strengh
private alias object.value2 : object.activePlayers
private alias object.value3 : onRamp
private alias object.value4 : object.timer

//-------Function declarations-------//

//-------Static Values-------//

//-------Tables-------//

private table TrickRamp_heightsTable
	 8,  8, 8, 8, 10, 12, 14
	16, 18, 20, 21, 23, 24, 26
	27, 29, 30, 32, 33, 34, 35
	36, 37, 38, 38, 38, 38, 38
end table

//-------Functions-------//
function TrickRamp_DebugDraw
	DrawSprite(8)
end function


function TrickRamp_DebugSpawn
	CreateTempObject(TypeName[Trick Ramp], 0, object.xpos, object.ypos)
	object[tempObjectPos].drawOrder = 4
end function

event ObjectMain
	temp2 = 0
foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
	switch object.state
	case 0
		temp0 = object[currentPlayer].xpos
		temp0 -= object.xpos
		temp0 += 0x1C0000
		temp0 >>= 17
		if temp0 < 0
			temp0 = 0
		end if
		if temp0 > 27
			temp0 = 27
		end if
		switch object.direction
		case FLIP_NONE
			GetTableValue(temp1, temp0, TrickRamp_heightsTable)
			FlipSign(temp1)
			temp4 = object[currentPlayer].speed
			temp5 = object[currentPlayer].xpos
			BoxCollisionTest(C_BOX, object.entityPos, -22, temp1, 24, -8, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
			switch checkResult
			case COL_LEFT
				object[currentPlayer].speed = temp4
				object[currentPlayer].xpos = temp5
				// [Fallthrough]
			case COL_NONE
			case COL_RIGHT
			case COL_BOTTOM
				GetBit(temp3, object.activePlayers, temp2)
				if temp3 == true
					temp3 = object[currentPlayer].xpos
					temp3 -= object.xpos
					temp3 >>= 16
					Abs(temp3)
					if temp3 < 28
						if object[currentPlayer].yvel >= 0
							object[currentPlayer].iypos = object[currentPlayer].collisionBottom
							FlipSign(object[currentPlayer].ypos)
							temp1 <<= 16
							object[currentPlayer].ypos += temp1
							object[currentPlayer].ypos += object.ypos
							checkresult = true
							object[currentPlayer].animation = 28
						end if
					end if
				end if
				break
			end switch
			break
		case FLIP_X
			temp0 -= 27
			Abs(temp0)
			GetTableValue(temp1, temp0, TrickRamp_heightsTable)
			FlipSign(temp1)
			temp4 = object[currentPlayer].speed
			temp5 = object[currentPlayer].xpos
			BoxCollisionTest(C_BOX, object.entityPos, -24, temp1, 22, -8, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
			switch checkResult
			case COL_RIGHT
				object[currentPlayer].speed = temp4
				object[currentPlayer].xpos = temp5
				// [Fallthrough]
			case COL_NONE
			case COL_LEFT
			case COL_TOP
				GetBit(temp3, object.activePlayers, temp2)
				if temp3 == true
					temp3 = object[currentPlayer].xpos
					temp3 -= object.xpos
					temp3 >>= 16
					Abs(temp3)
					if temp3 < 28
						if object[currentPlayer].yvel >= 0
							object[currentPlayer].iypos = object[currentPlayer].collisionBottom
							FlipSign(object[currentPlayer].ypos)
							temp1 <<= 16
							object[currentPlayer].ypos += temp1
							object[currentPlayer].ypos += object.ypos
							checkresult = true
						end if
					end if
				end if
				break
			end switch
			break
		end switch
		if checkresult == true
			onRamp = 1
		else
			onRamp = 0
		endif
		if onRamp == 1
			SetBit(object.activePlayers, temp2, true)

			if object.direction == FLIP_NONE
				BoxCollisionTest(C_TOUCH, object.entityPos, 20, -128, 22, 0, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
				if checkresult == true
					object.state = 2
				endif
			else
				BoxCollisionTest(C_TOUCH, object.entityPos, -23, -128, 21, 0, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
				if checkresult == true
					object.state = 2
				endif
			endif
		else
			SetBit(object.activePlayers, temp2, false)
		endif
		temp2++
		break
	case 2	
		if object.timer > 2
			object.state = 0
			checkresult = false
		else
			object.timer ++
		endif
		break
	endswitch
next

end event

event ObjectDraw
	DrawSpriteFX(8, FX_FLIP, object.xpos, object.ypos)
	object.timer ++
	if object.timer > 7
		object.timer = 0
		object.frame ++
	endif
	object.frame &= 7
	DrawSpriteFX(object.frame, FX_FLIP, object.xpos, object.ypos)
end event

event ObjectStartup
    	LoadSpriteSheet("Global/Items.gif")
	SpriteFrame(0, 0, 0, 0, 0, 0)			//00 Empty Frame
	SpriteFrame(2, -27, 15, 12, 464, 2)		//01 Animation Frame 01
	SpriteFrame(2, -27, 15, 12, 480, 2)		//02 Animation Frame 02
	SpriteFrame(2, -27, 15, 12, 496, 2)		//03 Animation Frame 03
	SpriteFrame(2, -27, 15, 12, 464, 15)		//04 Animation Frame 04
	SpriteFrame(2, -27, 15, 12, 480, 15)		//05 Animation Frame 05
	SpriteFrame(2, -27, 15, 12, 496, 15)		//06 Animation Frame 06
	SpriteFrame(2, -27, 15, 12, 464, 28)		//07 Animation Frame 07

	SpriteFrame(-22, -38, 45, 32, 418, 2)		//08 Trick Ramp


	SetTableValue(TypeName[Trick Ramp], DebugMode_ObjCount, DebugMode_TypesTable)
	SetTableValue(TrickRamp_DebugDraw, DebugMode_ObjCount, DebugMode_DrawTable)
	SetTableValue(TrickRamp_DebugSpawn, DebugMode_ObjCount, DebugMode_SpawnTable)
	DebugMode_ObjCount++
end event

event RSDKDraw
	DrawSprite(0)
end event

event RSDKLoad
	LoadSpriteSheet("Global/Display.gif")
	SpriteFrame(-16, -16, 32, 32, 223, 172)
end event